<template>
  <div class="menu-manager">
    <!-- 菜单查询表单 -->
    <div class="query-form">
      <el-form
        size="small"
        :inline="true"
        ref="queryForm"
        :model="queryForm"
        class="demo-form-inline"
      >
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="queryForm.menuName" placeholder="请输入菜单名称"></el-input>
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState">
          <el-select v-model="queryForm.menuState" placeholder="请输入菜单状态">
            <el-option label="正常" value="1"></el-option>
            <el-option label="停用" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery('queryForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 菜单管理列表 -->
    <div class="base-table">
      <!-- 新增按钮 -->
      <div class="action">
        <el-button type="primary" size="small" @click="handleOpen(1)">新增</el-button>
      </div>
      <!-- 菜单列表 -->
      <el-table
        :data="menuList"
        size="mini"
        row-key="_id"
        :tree-props="{ children: 'children' }"
        style="width: 100%; margin-bottom: 20px"
      >
        <el-table-column
          v-for="item in columns"
          :key="item._id"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          :formatter="item.formatter"
        >
        </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleOpen(2,scope.row)">新增</el-button>
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row._id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 菜单弹窗 -->
    <el-dialog title="菜单新增" :visible.sync="showModal">
      <el-form :model="menuForm" ref="menu" :rules="rules" label-width="100px">
        <el-form-item label="父级菜单" prop="parentId">
            <el-cascader placeholder="select" v-model="menuForm.parentId" :options="menuList" :props="{ checkStrictly: true , label : 'menuName', value : '_id' }" clearable></el-cascader>
            <span>不选，则直接创建一级菜单</span>
        </el-form-item>  
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="menuForm.menuType">
            <el-radio :label="1" >菜单</el-radio>
            <el-radio :label="2" >按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="menuForm.menuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路由地址" prop="path" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件路径" prop="component" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.component" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState" v-show="menuForm.menuType == 1">
          <el-radio-group v-model="menuForm.menuState">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>
         
        <el-form-item label="权限标识" prop="menuCode" v-show="menuForm.menuType == 2">
          <el-input v-model="menuForm.menuCode" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment";
export default {
  name: "",
  data() {
    return {
      queryForm: {
        menuState: "1",
        menuName: "",
      },
      menuForm: {
          action : "",
          parentId : [],
          menuName : "",
          menuState : 1,
          menuType : 1,
          icon : "",
          path : "",
          component : ""
      },
      rules : {
          menuName : [{required : true, message : "菜单名称不能为空", trigger : "blur"}]
      },
      showModal: false,
      menuList: [],
      columns: [
        {
          label: "菜单名称",
          prop: "menuName",
          width: 180,
        },
        {
          label: "图标",
          prop: "icon",
        },
        {
          label: "菜单类型",
          prop: "menuType",
          formatter(row, column, value) {
            //1 菜单 2按钮
            let menuType = { 1: "菜单", 2: "按钮" };
            return menuType[value];
          },
        },
        {
          label: "权限标识",
          prop: "",
        },
        {
          label: "路由地址",
          prop: "path",
        },
        {
          label: "组件路径",
          prop: "component",
        },
        {
          label: "菜单状态",
          prop: "menuState",
          formatter(row, column, value) {
            let menuState = { 1: "正常", 2: "停用" };
            return menuState[value];
          },
        },
        {
          label: "创建时间",
          prop: "createTime",
          formatter(row, column, value) {
            return moment.parseZone(value).local().format("YYYY-MM-DD HH:mm:ss");
          },
        },
      ],
      

    };
  },
  created() {
    //调用初始化菜单列表方法
    this.getMenuList();
  },
  methods: {
    //初始化菜单列表
    async getMenuList() {
      let res = await this.menuApi.getMenuList(this.queryForm);
      console.log(res);
      this.menuList = res;
    },
    //重置表单
    resetQuery(formName) {
      this.$refs[formName].resetFields();
    },
    //菜单表单查询
    handleQuery() {
      this.getMenuList();
    },
    //打开新增弹窗
    handleOpen(type,row) {
      this.showModal = true;
      this.menuForm.action = "add"
      
      if(type == 2){
          this.menuForm.parentId = [...row.parentId,row._id].filter((item)=>item)  
      }
    },
    //弹窗的确定按钮方法
    async handleSubmit(){
        let res = await this.menuApi.createMenu(this.menuForm);
        this.showModal = false;
        this.getMenuList();
        this.resetQuery("menu")
    },
    //打开编辑弹窗
    handleEdit(row){
        this.showModal = true;
        this.menuForm.action = "edit"
        this.$nextTick(()=>{
           Object.assign(this.menuForm,row) 
        })
    },
    //删除菜单
   async handleDelete(id){
       let res = await this.menuApi.createMenu({action : "delete", _id : id})
       console.log(res)
       this.$message.success("删除成功")
       this.getMenuList()
    }
  },
  components: {},
};
</script>

<style scoped></style>
